package com.chandra.pos.order.service.impl;


import com.chandra.pos.customer.dao.entity.CustomerEntity;
import com.chandra.pos.customer.dao.entity.CustomerRepository;
import com.chandra.pos.order.dao.OrderItemRepository;
import com.chandra.pos.order.dao.OrderRepository;
import com.chandra.pos.order.dao.entity.OrderEntity;
import com.chandra.pos.order.dao.entity.OrderItemEntity;
import com.chandra.pos.order.model.*;
import com.chandra.pos.order.service.OrderRole;
import com.chandra.pos.product.dao.entity.ProductEntity;
import com.chandra.pos.product.dao.entity.ProductRepository;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dnagarajan on 3/28/2015.
 */
@Component
public class OrderRoleImpl implements OrderRole {


    @Autowired
    private CustomerRepository customerRepository;


    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private OrderItemRepository orderItemRepository;

    @Autowired
    private ProductRepository productRepository;

    @Override
    public OrderModel saveOrder(OrderModel orderModel) {



        OrderEntity orderEntity = new OrderEntity();

        BeanUtils.copyProperties(orderModel, orderEntity);


        if (orderEntity.getCustomerId() != null) {

            CustomerEntity customerEntity = customerRepository.findOne(orderEntity.getCustomerId());
            orderEntity.setCustomerName(customerEntity.getFirstName());


        }

        orderEntity = orderRepository.save(orderEntity);
        orderModel.setOrderId(orderEntity.getOrderId());

        return orderModel;

    }

    @Override
    public OrderItemModel saveOrderItem(OrderItemModel orderItemModel) {
        OrderItemEntity orderItemEntity = new OrderItemEntity();

        BeanUtils.copyProperties(orderItemModel, orderItemEntity);
        ProductEntity productEntity = productRepository.getOne(orderItemModel.getProductId());
        orderItemEntity.setProductName(productEntity.getName());
        orderItemEntity.setProductSku(productEntity.getSku());

        orderItemEntity = orderItemRepository.save(orderItemEntity);
        orderItemModel.setItemId(orderItemEntity.getItemId());
        return orderItemModel;

    }


    public boolean removeOrderItem(OrderItemModel orderItemModel) {

        OrderItemEntity orderItemEntity = orderItemRepository.getOne(orderItemModel.getItemId());

        if (orderItemEntity == null)
            throw new EntityNotFoundException("Order item with ID:" + orderItemModel.getItemId() + "not found");

        orderItemRepository.delete(orderItemModel.getItemId());
        return true;
    }

    @Override
    public OrderList search(OrderFilter orderFilter) {

        OrderList customerList = new OrderList();

        List<OrderModel> customerModels = new ArrayList<>();
        Page<OrderEntity> customerEntity1 = null;

        PageRequest request =
                new PageRequest(orderFilter.getPage() - 1, orderFilter.getRows(), orderFilter.getDBSord(), orderFilter.getSidx());

        if (orderFilter.getId() != null) {
            OrderEntity orderEntity = orderRepository.findOne(orderFilter.getId());
            OrderModel customerModel = new OrderModel();

            BeanUtils.copyProperties(orderEntity, customerModel);

            customerModels.add(customerModel);

            customerList.setOrders(customerModels);
            return customerList;


        }
        if (orderFilter.getStatus() != null && orderFilter.getFromDate() != null && orderFilter.getToDate() != null)
            customerEntity1 = orderRepository.searchOrder(orderFilter.getStatus(), orderFilter.getFromDate(), orderFilter.getToDate(), request);
        else if (orderFilter.getFromDate() != null && orderFilter.getToDate() != null)
            customerEntity1 = orderRepository.findByOrderDateBetween(orderFilter.getFromDate(), orderFilter.getToDate(), request);
        else if (orderFilter.getStatus() != null)
            customerEntity1 = orderRepository.searchOrder(orderFilter.getStatus(), request);

        else
            customerEntity1 = orderRepository.findByOrderDateBetween(DateTime.now().withTimeAtStartOfDay(), DateTime.now().plusDays(1), request);


        for (OrderEntity customerEntity : customerEntity1.getContent()) {
            OrderModel customerModel = new OrderModel();

            BeanUtils.copyProperties(customerEntity, customerModel);

            customerModels.add(customerModel);
        }
        customerList.setPage(orderFilter.getPage());
        customerList.setRecords(customerEntity1.getTotalElements());
        customerList.setTotal(customerEntity1.getTotalPages());

        customerList.setOrders(customerModels);
        return customerList;

    }

    @Override
    public OrderModel getOrderById(Long orderId) throws EntityNotFoundException {

        OrderModel customerModel = new OrderModel();

        OrderEntity customerEntity1 = orderRepository.findOne(orderId);
        if (customerEntity1 == null)
            throw new EntityNotFoundException("dsds");
        BeanUtils.copyProperties(customerEntity1, customerModel);


        return customerModel;
    }

    @Override
    public OrderItemList listOrderItems(OrderItemModel orderItemModel) {
        OrderItemList orderItemList = new OrderItemList();
        List<OrderItemModel> orderItems = new ArrayList<>();


        List<OrderItemEntity> orderItemEntities = orderItemRepository.findByOrderId(orderItemModel.getOrderId());

        for (OrderItemEntity orderItemEntity : orderItemEntities) {
            OrderItemModel itemModel = new OrderItemModel();

            BeanUtils.copyProperties(orderItemEntity, itemModel);
            orderItems.add(itemModel);

        }
        orderItemList.setOrderItems(orderItems);
        return orderItemList;
    }

    @Override
    public OrderModel getOrderDetails(OrderFilter filter) throws EntityNotFoundException {

        OrderEntity orderEntity = orderRepository.findOne(filter.getId());

        OrderModel orderModel1 = new OrderModel();

        BeanUtils.copyProperties(orderEntity, orderModel1);
        return orderModel1;
    }
}
